package cameraClassThread.uweTronnier.fhkl.de;

import cameraClassThread.uweTronnier.fhkl.de.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

public class CameraClassThreadActivity extends Activity implements OnTouchListener {
	
	OurView v;
	Bitmap icon;
	float x, y;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        v = new OurView(this);
        v.setOnTouchListener(this);
        icon = BitmapFactory.decodeResource(getResources(), R.drawable.droid);
        x = y = 0;
        setContentView(v);       
    }
    
    @Override
	protected void onPause() {
		v.pause();
		super.onPause();

	}

	@Override
	protected void onResume() {
		super.onResume();
		v.resume();
		}
	
	@Override
	public boolean onTouch(View v, MotionEvent me) {

		switch (me.getAction()) {
		case MotionEvent.ACTION_DOWN:
			x = me.getX();
			y = me.getY();
			break;
			
		case MotionEvent.ACTION_UP:
//Experiment mit mehrern Fingern
//			x = me.getX();
//			y = me.getY();
			break;
			
		case MotionEvent.ACTION_MOVE:
			x = me.getX();
			y = me.getY();
			break;
		
		}
		
		return true;
	}	
		


	public class OurView extends SurfaceView implements Runnable {
    	
		Thread t = null;
		SurfaceHolder sh;
		boolean isThreadRunning = false;
		
    	public OurView(Context context) {
    		super(context);
    		sh = getHolder();
		}
    	
    	public void run() {
    		
    		while (isThreadRunning) {
    			//perform some drawing
    			if (!sh.getSurface().isValid()) {
    				continue; //next loop
    			}
    			Canvas c = sh.lockCanvas(); //now we can paint to the canvas
    			c.drawARGB(255, 150, 150, 10);
    			c.drawBitmap(icon, x-(icon.getWidth()/2), y-(icon.getHeight()/2), null);
       			Log.d(getLocalClassName() + "x,y: ", Float.toString( x ) + " " + Float.toString( y ));
    			
    			sh.unlockCanvasAndPost(c);
    				
    		}
    	};
    	
    	public void pause() {
    		isThreadRunning = false;
    		while (true){
    			try {
    				t.join();
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    			break;
    		}
    	};
    	
    	public void resume() {
    		isThreadRunning = true;
    		t = new Thread(this);
    		t.start();
    	};
    }


}